#include "wap_slider.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_slider.h"
#include "wa_label.h"
#include "wap.h"
#include "stdio.h"

static void wap_slider_create(void);
static void wap_slider_del(void);
static void wap_slider_callback(wa_sign* sign);

wa_screen_obj wap_slider_screen = 
{
	.id = WAP_WIN_SLIDER_ID,
	.create = wap_slider_create,
	.del = wap_slider_del,
};

wa_obj* wap_slider_win;
wa_obj* wap_slider_slider;
wa_obj* wap_slider_label_title;
wa_obj* wap_slider_label_value;

extern wa_font font_song_16_ascii;

uint8_t wap_slider_value;

void wap_slider_init(void)
{
	wap_slider_value = 0;
}

static void wap_slider_create(void)
{
	char str[4];
	wap_slider_win = wa_win_create(NULL);
	wa_win_set_pos(wap_slider_win, 0, 0);
	wa_win_set_size(wap_slider_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_slider_win, wa_nok);
	wa_win_set_callback(wap_slider_win, wap_slider_callback);
	wap_slider_screen.win = wap_slider_win;
	
	wap_slider_label_title = wa_label_create(NULL);
	wa_win_add_obj(wap_slider_win, wap_slider_label_title);
	wa_obj_set_pos(wap_slider_label_title, 0, 8);
	wa_obj_set_size(wap_slider_label_title, WA_LCD_W, 16);
	wa_label_set_font(wap_slider_label_title, &font_song_16_ascii);
	wa_label_set_text(wap_slider_label_title, "slider");
	wa_label_set_align_mode(wap_slider_label_title, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
	
	wap_slider_label_value = wa_label_create(NULL);
	wa_win_add_obj(wap_slider_win, wap_slider_label_value);
	wa_obj_set_pos(wap_slider_label_value, 0, 40);
	wa_obj_set_size(wap_slider_label_value, WA_LCD_W, 16);
	wa_label_set_font(wap_slider_label_value, &font_song_16_ascii);
	wa_label_set_align_mode(wap_slider_label_value, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
	sprintf(str, "%d", wap_slider_value);
	wa_label_set_text(wap_slider_label_value, str);
	
	wap_slider_slider = wa_slider_create(NULL);
	wa_win_add_obj(wap_slider_win, wap_slider_slider);
	wa_obj_set_pos(wap_slider_slider, 16, 28);
	wa_obj_set_size(wap_slider_slider, 96, 8);
	wa_slider_set_direction(wap_slider_slider, WA_SLIDER_DIR_LEFT);
	wa_slider_set_limit_value(wap_slider_slider, 0, 100);
	wa_slider_set_value(wap_slider_slider, wap_slider_value);
	wa_slider_set_back_color(wap_slider_slider, WA_COLOR_BACKGROUND);
	wa_slider_set_fore_color(wap_slider_slider, WA_COLOR_FOREGROUND);
	wa_slider_set_slider_width(wap_slider_slider, 6);
	wa_slider_set_twig_width(wap_slider_slider, 1);
}

static void wap_slider_del(void)
{
	wa_obj_del(wap_slider_win);
}

static void _wap_slider_paint(wa_obj* obj)
{
}

static void wap_slider_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_slider_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			char str[4];
			if((wap_slider_value + 10) >= 100)
			{
				wap_slider_value = 100;
			}
			else
			{
				wap_slider_value += 10;
			}
			sprintf(str, "%d", wap_slider_value);
			wa_label_set_text(wap_slider_label_value, str);
			wa_slider_set_value(wap_slider_slider, wap_slider_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			char str[4];
			if(wap_slider_value <= 10)
			{
				wap_slider_value = 0;
			}
			else
			{
				wap_slider_value -= 10;
			}
			sprintf(str, "%d", wap_slider_value);
			wa_label_set_text(wap_slider_label_value, str);
			wa_slider_set_value(wap_slider_slider, wap_slider_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			char str[4];
			if(wap_slider_value > 0)
			{
				wap_slider_value--;
			}
			sprintf(str, "%d", wap_slider_value);
			wa_label_set_text(wap_slider_label_value, str);
			wa_slider_set_value(wap_slider_slider, wap_slider_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			char str[4];
			if(wap_slider_value < 100)
			{
				wap_slider_value++;
			}
			sprintf(str, "%d", wap_slider_value);
			wa_label_set_text(wap_slider_label_value, str);
			wa_slider_set_value(wap_slider_slider, wap_slider_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}

